package com.edu.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.edu.dao.BaseDao;
import com.edu.dao.UserDao;
import com.edu.entiy.User;

public class UserDaoImpl implements UserDao {

	@Override
	public User selectUser(String userName, String password) {
		String sql = "select * from user where userName = ? and password = ?";
		Object[] params = {userName,password};
		return BaseDao.executeQuaryOne(sql, params, User.class);
	}

	@Override
	public List<User> selectUsers() {
		String sql = "select * from user where status != 2";
		Object[] params = {};
		return BaseDao.executeQuaryList(sql, params, User.class);
	}

	@Override
	public void addUser(User user) {
		// TODO Auto-generated method stub
		String sql = "insert into user(UserName,nickname,password,email,phone,times,status,lendDayNum,lendMaxNum) value(?,?,?,?,?,?,?,?,?)";
		Object[] params = {user.getUserName(),user.getNickname(),user.getPassword(),user.getEmail(),user.getPhone(),user.getTimes(),user.getStatus(),user.getLendDayNum(),user.getLendMaxNum()};
		BaseDao.executeUpdate(sql, params);
	}

	@Override
	public User selectUserByUserName(String userName) {
		// TODO Auto-generated method stub
		String sql = "select * from user where userName = ?";
		Object[] params = {userName};
		return BaseDao.executeQuaryOne(sql, params, User.class);
	}

	@Override
	public User selectUserByPhone(String phone) {
		// TODO Auto-generated method stub
		String sql = "select * from user where phone = ?";
		Object[] params = {phone};
		return BaseDao.executeQuaryOne(sql, params, User.class);
	}

	@Override
	public User selectUserByEmali(String email) {
		// TODO Auto-generated method stub
		String sql = "select * from user where email = ?";
		Object[] params = {email};
		return BaseDao.executeQuaryOne(sql, params, User.class);
	}

	@Override
	public int selectUserCount() {
		// TODO Auto-generated method stub
		String sql = "select count(*) from user where status != 2";
		int count = BaseDao.executeQuaryCount(sql);
		return count;
	}

	@Override
	public List<User> selectPageBrand(int begin, int size) {
		// TODO Auto-generated method stub
		String sql = "select * from user where status !=2 limit ?,?";
		Object[] params = {begin,size};
		return BaseDao.executeQuaryList(sql, params, User.class);
	}

	@Override
	public int updateUser(User user) {
		// TODO Auto-generated method stub
		String sql = "update user set id = "+user.getId();
		List<Object> list = new ArrayList<Object>();
		if(user.getPhone()!=null) {
			sql +=",phone = ? ";
			list.add(user.getPhone());
		}
		if(user.getLendDayNum()!=0) {
			sql +=",lendDayNum = ? ";
			list.add(user.getLendDayNum());
		}
		if(user.getLendMaxNum()!=0) {
			sql +=",lendMaxNum = ? ";
			list.add(user.getLendMaxNum());
		}
		if(user.getPassword() != null && user.getPassword()!="") {
			sql +=",password = ? ";
			list.add(user.getPassword());
		}
		list.add(user.getId());
		sql += " where id = ? ";
		Object[] params = list.toArray();
		return BaseDao.executeUpdate(sql, params);
	}

	@Override
	public int deleteUser(int id) {
		// TODO Auto-generated method stub
		String sql = "delete from user where id = ?";
		Object[] params = {id};
		return BaseDao.executeUpdate(sql, params);
	}

	@Override
	public int updateUserBorrowBook(User user) {
		// TODO Auto-generated method stub
		String sql = "update user set times = ?,lendMaxNum = ? where id = ?";
		Object[] params = {user.getTimes(),user.getLendMaxNum(),user.getId()};
		return BaseDao.executeUpdate(sql, params);
	}

	@Override
	public User selectUserById(int id) {
		// TODO Auto-generated method stub
		String sql = "select * from user where id = ?";
		Object[] params = {id};
		return BaseDao.executeQuaryOne(sql, params, User.class);
	}

	@Override
	public List<User> selectUsersOrderByTimes(int begin, int size) {
		// TODO Auto-generated method stub
		String sql = "select * from user where status !=2 order by times desc limit ?,?";
		Object[] params = {begin,size};
		return BaseDao.executeQuaryList(sql, params, User.class);
	}

	@Override
	public List<User> selectUsersOrderByTimes(int begin, int size, String word) {
		// TODO Auto-generated method stub
		StringBuffer stringBuffer = new StringBuffer("select * from user where status !=2");
		if(word != null && word !="") {
			stringBuffer.append(" and (nickname like'%"+word+"%'");
			stringBuffer.append(" or userName like'%"+word+"%')");
		}
		stringBuffer.append(" order by times desc limit ?,?");
		Object[] params = {begin,size};
		return BaseDao.executeQuaryList(stringBuffer.toString(), params, User.class);
	}

	@Override
	public int selectUsersCount(String word) {
		// TODO Auto-generated method stub
		StringBuffer stringBuffer = new StringBuffer("select count(*) from user where status !=2");
		if(word != null && word !="") {
			stringBuffer.append(" and (nickname like'%"+word+"%'");
			stringBuffer.append(" or userName like'%"+word+"%')");
		}
		String sql = stringBuffer.toString();
		return BaseDao.executeQuaryCount(sql);
	}

	@Override
	public int updatePass(int userId, String password) {
		// TODO Auto-generated method stub
		String sql = "update user set password = ? where id = ?";
		Object[] params = {password,userId};
		return BaseDao.executeUpdate(sql, params);
	}
	
	@Override
	public int updateUserPhone(User user) {
		// TODO Auto-generated method stub
		String sql = "update user set nickname= ? ,phone= ? where id= ?";
		Object[] params = {user.getNickname(),user.getPhone(),user.getId()};
		return BaseDao.executeUpdate(sql, params);
	}

	
}
